package com.bdsy.system.web.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.bdsy.common.annotation.Json;
import com.bdsy.common.controller.AbstractController;
import com.bdsy.common.utils.Result;
import com.bdsy.system.annotation.SysLog;
import com.bdsy.system.model.Authorization;
import com.bdsy.system.service.AuthorizationService;
import com.bdsy.system.service.UserOperationLogService;

/**
 * ClassName:UserController
 * Description: TODO 添加描述.
 * @author   Administrator
 * @version  
 * @since    JDK 1.8
 */
@Controller
@RequestMapping(value = "${web.adminPath}")
public class AuthorizationController extends AbstractController{
    
	@Autowired
	UserOperationLogService logService;
	
	@Autowired
	AuthorizationService authorizsationService;
    
	@RequestMapping(value="sysauth")
	public String sysuser(HttpServletRequest request, HttpServletResponse response, Model model) {
	   
		List<Map> list = authorizsationService.getAuthParnet();
		model.addAttribute("list", list);
		return "admin/system/sysauth";
	}
	
	@Json
	@RequestMapping(value="authlist")
	public Object userlist(HttpServletRequest request, HttpServletResponse response, Model model) {
		List<Map> list = authorizsationService.getAuth();
		for(Map m : list){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if(m.get("create_date") != null && !"".equals(m.get("create_date"))){
				m.put("create_date", sdf.format(m.get("create_date")));
			}
		}
		return list;
	}
	
	@Json
	@SysLog("新增菜单")
	@RequiresPermissions("sys:auth:save")
	@RequestMapping(value="addAuth")
	public Object addAuth(Authorization authorization) {
		authorization.setCreateDate(new Date());
		boolean result = authorizsationService.addAuth(authorization);
		if (result) {
    		return Result.ok("添加成功");
		}else{
			return Result.error("添加失败");
		}
	}
	
	@Json
	@SysLog("修改菜单")
	@RequiresPermissions("sys:auth:update")
	@RequestMapping(value="updateAuth")
	public Object updateAuth(Authorization authorization) {
		boolean result = authorizsationService.updateAuth(authorization);
		if (result) {
    		return Result.ok("修改成功");
		}else{
			return Result.error("修改失败");
		}
	}
	
	@Json
	@SysLog("删除菜单")
	@RequiresPermissions("sys:auth:delete")
	@RequestMapping(value="deleteAuthById")
	public Object deleteAuthById(String pIds) {
		List<String> list = JSON.parseArray(pIds, String.class);
		boolean result = authorizsationService.deleteAuthById(list);
		if (result) {
    		return Result.ok("删除成功");
		}else{
			return Result.error("删除失败");
		}
	}
}

